package com.hue.gulimail.gulimailorder.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {
    //在支付宝创建的应用的id
    private String app_id = "2021000117658883";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCEpcXwpo5ybXi0JN/kduYWwUfeDptl39HMGpvIOzCsXbNrk8MzvBsl6JRRwfGdLJAvXsLizuQj6oa12u5i+RQRKp8X2QX+IR5Hr/Bgmeui6p5V5Ia9HgIM4nVw7cbimN2RaY6H1mdiw6zhSqHpWgqia1nyqk1AWr8hJ8l4T6u47F6DDIeMx+rPFL54MniDmfIpc00BxSXM83KjOuMzIkceSRpfKUDpX0LDUsHV6lrgzj+2y/JK0LtqMTEZaDl0yV6p7OQOCo+zK+nU/Vo8YEHnNZVLzDlSp/5BspBpRFsDz7pEz157m6yjLsQlifF2t+OtcmwCJ8zwpHM8dLn27m/TAgMBAAECggEAJa1MfuFgPqJXkZuTuLoI+teliS5O923OkpHQQ9yEI8FtsBXFcVFxyJ+6WUqkTUeWIM5ivreTV5xK7SalZZloGMQpckWjjR5tEmGn/lhZ9Ol9VdpWZys5290LDKuNPVm8PYxk/rJopUX9OfTP7o8zzBR/9QxoauobtdguQq0mkpysiWLjWkPGJOydmpjKd+1xip6PR4lii52CCRhxBvgpb39mfqrtkGBUnp0Jj/NN8tvW4FNa7GQmh98GhDKtLr8VBX8lkRg1kdIoJpRwmBM79YFjRlMIAjUnBsKQLtkFeL4dmzrom6V38U2GVXXtGBpg0s0J1oWV/J881kF6Q8i6+QKBgQDwWFaQ1/C9wPe0ocgAuPaMceQL5dTXapvgdpK4JpcHmN/DZJ+9g9BSpJwQVNrzBWKLICTe2LcxAUePLzMnjUZhHbsuAKFLu+AGLjR68XYnVqkexzk5BGflAfxZHivcD9PYR0z3VSyoN4GD5UzSuFjfsGf4X4OgpfJumj9ovxrOjwKBgQCNSZ7H5CykJ5YJwItSR22ljfiZRi1BZuSz0XHbwtQoU1rtaii/oLjwDU7Rx/SThMGeuFCxq4PK2mAonbbN67BSRJVYlo1+ab9Mo/wccTeVg6Tq6/XOqAmKMrixM8hbSF+/XvANeigrulx9CVl7yqjFO+ebs7O/V8HMqySZ2rksfQKBgQDfmK9gLSUQw3cvGttgoLLuUf5gez8Z8Vp25pj0rSIQtU5nDzP1Ma4bVHzETM0tDbNxz0O99kvnUVNrh/PFNg9lxi0fTQLB3FU8Krw0LHNJgbvwoFNCEjE9wi0sE3+mSStCqIKYDytui8T4GdjwqQQ1hn4TeXiRehEiiGm5XyDnTQKBgCy2U61mpEn+zRig+nY7bJOr7jRzZPcBnPbbHXEyovlwB6KCqgqazmwJihwRb3EP3kZghgj0Am071pqD7soUAGTIl4rFelm7FXMnoi1OFw0V8Cb4LX4M55f4nUJW5N0edk6VuNpLAlN0UrivMMZgSDDC2HUJ1aSEHSysLJUUBu3xAoGALvOkOD0fQ5vUTJtPxn7oAfUZe0lJRj/z9utv1nw6/eUVm98AXXTuiTkraauMcZJ0sLCnAxbu7mVQljbNlcJqv61WrQlvNEJ6V17FpMqlVf2nWfAo5J93n4/ShmB1a7HVMrEuZo+kb4YEy40yukAnHixoH3F6bh+RswePhb4Xvts=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyQQceVUChTJGtF/a8SXufhSxDTKporieTq9NO7yDZSpDlAX1zVPT/nf0KWAlxq1TYappWMIYtyrOABhJyn6flNP6vuSBiM5lYsepHvYrtRHqlFiJruEkiaCgEZBKL5aCfBHYj0oqgQn9MpNV/PEH4cBYAVaiI4+VX8CBUQfeEGjgN6OkpLULZ3X0JUkmSnVvCNJ1m3PD68IIlbOfEZXJUKCqmZhzprGR5VWswjxA+g87cMwvijL4gdkSy/daG62Bz5vApcmmMkuX1k1fMWP4ajZCASVw8HD+MSLRhd8We9F97gd8CW0TavzbdR+mTS5H4yEgO8F9HRAsbkhV9yu0yQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    private String notify_url = "http://47z3384k71.qicp.vip/gulimailorder/order/paySuccess";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    private String return_url = "http://member.gulimail.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    /**
     * 支付方法,会返回阿里的支付页面
     *
     * @param vo
     * @return
     * @throws AlipayApiException
     */
    public String pay(PayVo vo) throws AlipayApiException {
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求,设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;
    }
}
